#ifndef __PUBLIC_H__
#define __PUBLIC_H__
#include "stm32f0xx_hal.h"
#include "stm32f0xx_hal_uart.h"
#include <string.h>

#define IO_OUTPUT_OID 0X0002
#define IO_INPUT_OID  0X0001
#define SET_FLAG     0X01

#pragma pack (1)
typedef struct {
	uint8_t     ol;        
	uint16_t    oid;        
	uint8_t 	rwFlag;	
	uint8_t 	opt2;
}TF_MON_OBJ ;
typedef struct{
	uint8_t        startFlag;
	uint8_t        packetLength[2];
	uint8_t        packetNum[2];
	uint8_t        devId;
	uint8_t        packetCmd;
	uint8_t        packetAnswer;
}COM_HEAD_TYPE;

typedef struct {
uint8_t            devVal[4]; 
uint8_t            serVal[4]; 
uint8_t            setSevFlag; 
uint8_t            setDevFlag; 	
}TBL_VALUE;

enum {IDLE,WORKING};
#define TABLEN 2

#define PACKET_HEAD_LEN (sizeof(COM_HEAD_TYPE))
#pragma pack (1)

#define BigtoLittle16(A)                 ((((uint16_t)(A) & 0xff00) >> 8) | \
                                                                   (((uint16_t)(A) & 0x00ff) << 8))

#define HIGH_ORDER          1          
#define LOW_ORDER           0
#define POLYNOME            0x1021      

#define SUCC_FLAG	0

#define mCmd_startFlag      0x7e
#define mCmd_endFlag        0x7e
#define mCmd_getData        0x01      
#define mCmd_setData        0x02       
#define mCmd_report         0x03  
#define mCmd_retransmission 0x04  

#define MCU_UART_IDX         0
#define BLE_UART_IDX         1

#define mRsp_success        0x00        

#define mRsp_condition      0x01       
#define mRsp_cmdError       0x02        


typedef union
{
    unsigned char   buffer[1050];
    COM_HEAD_TYPE   head;
} COM_TYPE;



typedef union         
{                     
	uint16_t    word;
	uint8_t    byte[2];
} UNION_2_BYTE;

extern void MX_SPI1_Init(void);
extern void MX_SPI2_Init(void);
extern void MX_USART1_UART_Init(void);
extern void MX_USART2_UART_Init(void);
#define SPITXBUFFERLEN 255
extern SPI_HandleTypeDef hspi1;
extern SPI_HandleTypeDef hspi2;
extern uint8_t aSpiTxBuffer[];
extern uint8_t aSpiRxBuffer[];

extern UART_HandleTypeDef huart1;
extern UART_HandleTypeDef huart2;

extern uint16_t g_sysComLedFlashCount;
extern uint16_t g_timeTest;
extern uint16_t g_timeReport;
extern uint16_t g_timeReportAll;
extern uint16_t g_reportCommTimeOut;
extern uint8_t g_firstCommWithServerOKFlag;
extern uint8_t sendTestData[];
extern const TF_MON_OBJ  monObj_tbl[] ;
extern TBL_VALUE  monObj_value[];
extern uint16_t search_table_index( uint16_t oid);
extern void uartSentTest(void);
extern void reportTreat( void);
extern void IO_outputTreat(void);
extern void led_treat(void);
extern void uart_treat(void);
extern void uartSent(uint8_t len);
#endif

